﻿
@{
    ViewBag.Title = "新增文章";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
<script src="~/Content/ueditor1.4.3.3/ueditor.config.js"></script>
<script src="~/Content/ueditor1.4.3.3/ueditor.all.min.js"></script>
<style>
    .input-width {
        width: 400px;
    }
</style>
<div class="layui-row">
    <div class="layui-col-md1">
        <div style="width:100%;opacity: 0;">
            W
        </div>
    </div>
    <div class="layui-col-md10">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;text-align:center;">
            <legend>@ViewBag.Edit</legend>
        </fieldset>
        <form class="layui-form layui-form-pane">
            <div class="layui-col-md5">
                <div class="layui-form-item">
                    <label class="layui-form-label">文章标题</label>
                    <div class="layui-input-block">
                        <input type="text" id="c_title" maxlength="100" value="@ViewBag.c_title" name="c_title" autocomplete="off" placeholder="请输入标题" class="layui-input">
                        <input type="hidden" name="hid_title" id="hid_title" value="@ViewBag.c_title" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">文章类别</label>
                    <div class="layui-input-block">
                        <select id="c_type" name="c_type"></select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">文章作者</label>
                    <div class="layui-input-block">
                        <input type="text" id="c_author" value="@ViewBag.c_author" name="c_author" lay-verify="required" placeholder="请输入文章作者" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">文章来源</label>
                    <div class="layui-input-block">
                        <input type="text" id="c_soures" name="c_soures" value="@ViewBag.c_soures" lay-verify="required" placeholder="请输入文章来源" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">添加日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="c_create_date" id="c_create_date" placeholder="请选择日期" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">文章描述</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入文章描述" class="layui-textarea" id="c_describe" name="c_describe">@ViewBag.c_describe</textarea>
                    </div>
                </div>
            </div>
            <div class="layui-col-md1">
                <div style="width:100%;opacity: 0;">
                    W
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">文章封面</label>
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="btn_img">上传图片</button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" src="@ViewBag.c_cover_img" width="150" height="150" id="c_cover_img">
                            <p id="demoText"></p>
                            <input type="hidden" id="svaePath" value="@ViewBag.c_cover_img" />
                        </div>
                    </div>
                </div>
               
            </div>
            <div class="layui-row">
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">文章内容</label>
                    <div class="layui-input-block">
                        <script id="editor" type="text/plain" style="width:100%;height:600px;">
                        </script>
                    </div>
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn" onclick="add_Content()" type="button" lay-filter="demo2">提交</button>
                    <a href="/Admin/Content/ContentManager" class="layui-btn">返回</a>
                </div>
            </div>
        </form>
    </div>
    <div class="layui-col-md1">
        <div style="width:100%;opacity: 0;">
            W
        </div>
    </div>
</div>



<script>
    var edior = UE.getEditor("editor");
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate
        , upload = layui.upload;
    var date = "";
    if ("@ViewBag.c_create_date" != "") {
        date = "@ViewBag.c_create_date";
    } else {
        date = new Date();
    }
    //日期
    laydate.render({
        elem: '#c_create_date'
        , value: date
        , type:'datetime'
        , format: 'yyyy-MM-dd' //可任意组合
    });

    //填充文章类别数据
    bind_content_type();
    function bind_content_type() {
        $.get_json("/Admin/Content/ContentTypeList/", { pageSize: 99 }, function (data) {
            var type = data.data;
            var html = "";
            for (var i = 0; i < type.length; i++) {
                html += "<option value=\"" + type[i].ct_id + "\">" + type[i].ct_name + "</option>";
            }
            $("#c_type").html(html);
            form.render();
            if ("@ViewBag.c_type" != "") {
                $("#c_type").val("@ViewBag.c_type");
            }
        });
    }

    function add_Content() {
        var c_type = $("#c_type").val();
        var c_title = $("#c_title").val();
        var c_author = $("#c_author").val();
        var c_soures = $("#c_soures").val();
        var c_create_date = $("#c_create_date").val();
        if (c_title == "") {
            layer.alert("请输入文章标题");
            return
        }
        //if (c_author == "") {
        //    layer.alert("请输入文章作者");
        //    return
        //}
        if (c_soures == "") {
            layer.alert("请输入文章来源");
            return
        }
        if (c_create_date == "") {
            layer.alert("请选择日期");
            return
        }
        //获取编辑器的内容（包括html标签）
        var c_content = UE.getEditor('editor').getContent();
        if (c_content.length == 0 || c_content == "") {
            layer.alert("文章内容不能为空");
            return;
        }
        //获取纯文本内容（不包括HTML标签）
        var msg = UE.getEditor('editor').getContentTxt();
        //如果没写摘要则截取文章内容的前一百个字
        if ($("#c_describe").val() == "") {
            $("#c_describe").val(msg.substring(0, 100));
        }
        $.post_str("/Admin/Content/add_Content", { c_id: "@ViewBag.c_id", c_type: c_type, c_title, c_title, c_author: c_author, c_soures: c_soures, c_create_date: c_create_date, c_content: c_content, hid_title: $("#hid_title").val(), c_describe: $("#c_describe").val(), c_cover_img: $("#svaePath").val() }, function (data) {
            if (data === "1") {
                layer.msg("保存成功", function () {
                    location.href = "/Admin/Content/ContentManager";
                });
            } else
                layer.alert(data);
        })

    }

    Load();
    function Load(){
        var edior = UE.getEditor("editor");
        //富文本编辑器要先实例化，否则会报错
        var content = "";
        if ("@ViewBag.have" == "yes") {
            var content = @Html.Raw(ViewBag.c_content)
        } 
        if (content == undefined || content == "" || content=="no") {
            content = "";
        }
        edior.ready(function () {
            edior.setContent(content, false);
        });
    }

    
    //普通图片上传
    var uploadInst = upload.render({
        elem: '#btn_img'
        , exts: 'jpg|png|gif|bmp|jpeg'
        , size: 500
        , url: '/Admin/Content/Uploag_img?c_id='+'@ViewBag.c_id'
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#c_cover_img').attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            //如果上传失败
            if (res.code === "1") {
                $("#svaePath").val(res.svaePath);
                return layer.msg('上传成功');
            }
            //上传成功
        }
        , error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });

</script>